<template>
  <section>
    <!-- 面包屑 -->
    <breadcrumbs></breadcrumbs>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">协议列表</div>
      </div>
      <el-table :data="tableData"
                style="width: 100%"
                row-key="dictId"
                border
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="80">
        </el-table-column>
        <el-table-column prop="title"
                         label="协议标题">
        </el-table-column>
        <el-table-column prop="terminal"
                         width="100"
                         label="终端">
            <template slot-scope="scope">
            <p v-if="scope.row.terminal == 1">回收站</p>
            <p v-if="scope.row.terminal == 2">大区营运</p>
            <p v-if="scope.row.terminal == 3">大区仓库</p>
            <p v-if="scope.row.terminal == 4">小程序</p>
           </template>
        </el-table-column>
        <el-table-column prop="content"
                         width="1000"
                         label="协议内容">
           
        </el-table-column>
        
        <el-table-column prop="updateTime"
                         label="添加时间">
        </el-table-column>
        
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    
    <el-dialog title="编辑协议"
               :visible.sync="dialogEditFormVisible">
      <el-form :model="editForm"
               label-width="120px"
               ref="editForm"
               :rules="rules">
        <el-form-item label="协议标题"
                      prop="title">
          <el-input v-model="editForm.title"
                    maxlength="25"
                    show-word-limit
                    autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item prop="terminal"
                         label="终端">
               <el-select v-model="editForm.terminal" placeholder="请选择协议终端">
                    <el-option label="回收站" value="1"></el-option>
                    <el-option label="大区营运" value="2"></el-option>
                     <el-option label="大区仓库" value="3"></el-option>
                    <el-option label="小程序" value="3"></el-option>
                </el-select>
        </el-form-item>
        <el-form-item label="协议内容"
                      prop="content">
          <el-input v-model="editForm.content"
                    type="textarea"
                    maxlength="300"
                    show-word-limit
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editQuestions('editForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加协议"
               :visible.sync="dialogCreateFormVisible">
      <el-form :model="createForm"
               label-width="120px"
               ref="createForm"
               :rules="rules">
        <el-form-item label="协议标题"
                      prop="title">
          <el-input v-model="createForm.title"
                    maxlength="25"
                    show-word-limit
                    autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item prop="terminal"
                         label="终端">
               <el-select v-model="createForm.terminal" placeholder="请选择协议终端">
                    <el-option label="回收站" value="1"></el-option>
                    <el-option label="大区营运" value="2"></el-option>
                     <el-option label="大区仓库" value="3"></el-option>
                    <el-option label="小程序" value="3"></el-option>
                </el-select>
        </el-form-item>
        <el-form-item label="协议内容"
                      prop="content">
          <Editor v-model="createForm.content" />
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogCreateFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addProblem('createForm')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import btns from '@/components/btns.vue';
import Editor from '@/components/edit';
import breadcrumbs from "@/components/breadcrumbs.vue";
export default {
  components: {
    btns,
    Editor,
    breadcrumbs,
  },
  data () {
    var telValidate = (rule, value, callback) => {

      if (value != null && value != '') {
        var tel = /^0\d{2,3}-?\d{7,8}$/;
        var phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (value.length == 11) {//手机号码
          if (phone.test(value)) {
            console.log(value);
            return true;
          }
        } else if (value.length == 13 && value.indexOf("-") != -1) {//电话号码
          if (tel.test(value)) {
            console.log(value);
            return true;
          }
        }
        callback('请输入正确的电话号码！');
      } else {
        callback();
      }
    };
    return {
      tableSelection: [],
      dialogEditFormVisible: false,
      tableData: [],
      editForm: {},
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogCreateFormVisible: false,
      createForm: {},
      rules: {
        "title": [
          { required: true, message: '请输入协议标题', trigger: 'blur' },
        ],
        "terminal": [
          { required: true, message: '请输入协议终端', trigger: 'blur' },
        ],
       
        "content": [
          { required: true, message: '请输入协议内容', trigger: 'blur' },
        ],

      },
      formLabelWidth: '120px',
      addfile: '',
      importFileUrl: this.GLOBAL.imgBaseUrl,
    }
  },
  created () {
    this.getList();
  },
  methods: {
      exec (fun) {
            if (this[fun]) {
                this.funTit = fun;
                this[fun]()
            } else {
                this.$message.error('方法未定义');
            }
        },
      
        update () {
            if (this.tableSelection.length != 1) {
                this.$message.error('请选中一行数据');
                return
            }
            let id = this.tableSelection[0].bannerId;
            this.$router.push({
                path: `/banner/edit`,
                query: { id: id }
            })
        },
        enable () {
            if (this.tableSelection.length != 1) {
                this.$message.error('请选中一行数据');
                return
            }
            var demo = this.tableSelection[0];
            if (demo.bannerStatus == 1) {
                this.$message.error('该数据已启用');
                return
            }
            this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$api.banner.status({
                    bannerIds: demo.bannerId,
                    status: 1
                }).then(d => {
                    if (d.code == 1) {
                        this.$message.success('启用成功');
                        this.loadData();
                    } else {
                        this.$message.error(d.msg);
                    }
                }).catch((e) => {
                    console.log(e);
                })
            });
        },
        disable () {
            if (this.tableSelection.length != 1) {
                this.$message.error('请选中一行数据');
                return
            }
            var demo = this.tableSelection[0];
            if (demo.bannerStatus == 0) {
                this.$message.error('该数据已禁用');
                return
            }
            this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$api.banner.status({
                    bannerIds: demo.bannerId,
                    status: 0
                }).then(d => {
                    if (d.code == 1) {
                        this.$message.success('禁用成功');
                        this.loadData();
                    } else {
                        this.$message.error(d.msg);
                    }
                }).catch((e) => {
                    console.log(e);
                })
            });
        },
        del () {
            if (this.tableSelection.length != 1) {
                this.$message.error('请选中一行数据');
                return
            }

            var demo = this.tableSelection[0];
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$api.agreement.delxieyi({
                    ids: this.tableSelection[0].id,
               
                }).then(d => {
                    if (d.code == 200) {
                        this.$message.success('删除成功');
                        this.getList();
                    } else {
                        this.$message.error(d.msg);
                    }
                }).catch((e) => {
                    console.log(e);
                })
            });
        },



    editQuestions (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.agreement.updatexieyi({
            id: this.tableSelection[0].id,
            title: this.editForm.title,
            content: this.editForm.content,
             terminal:this.editForm.terminal,
          }).then(d => {
            if (d.code == 200) {
              this.$message.success('修改成功');
              this.dialogEditFormVisible = false;
              this.getList();
            }
          })
        }
      })

    },
    getList () {
      this.$api.agreement.xieyilist({
        pageNo:this.currentPage,
        pageSize:this.pageSize

      }

      ).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
          this.total = d.totalCount;
        }
      })
    },
    add(e){
      this.dialogCreateFormVisible = true;
      console.log("createForm========="+this.createForm.content);
      this.createForm.content.setValue = "",
      this.createForm = {
                "terminal": '',
                "title": '',
                "content": "",   
            }
    },
    addProblem (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.agreement.addxieyi({

            terminal:this.createForm.terminal,
            title: this.createForm.title,
            content: this.createForm.content
          }).then(d => {
            if (d.code == 200) {
              this.$message.success('添加成功');
              this.dialogCreateFormVisible = false;
              this.getList();
            }
          })
        }
      })

    },
    //打开新增模态框
   
    //打开编辑模态框
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.editForm = this.tableSelection[0];
      this.editForm.content = this.tableSelection[0].content;
      this.editForm.terminal = this.tableSelection[0].terminal;
      // console.log(this.editForm);
      this.dialogEditFormVisible = true;
    },
    //删除数据
    // del () {
    //   if (this.tableSelection.length != 1) {
    //     this.$message.error('请选中一行数据');
    //     return
    //   }
    //   // console.log(this.ta);
    //   this.$confirm('确认要删除该商品吗, 是否继续', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     this.$api.agreement.addConfig({
    //       id: this.tableSelection[0].pactionId
    //     }).then((d) => {
    //       if (d.code == 200) {
    //         this.$message.success('删除成功');
    //         this.getList();
    //       } else {
    //         this.$message.error(d.msg);
    //       }
    //     })
    //     // console.log(this.tableSelection[0].enable);

    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消删除'
    //     });
    //   });
    // },
    //启用
    enable () { },
    //禁用
    disable () { },
    search () { },

    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
   tableSelectionChange(val) {
      this.tableSelection = val;
    },
    handleSizeChange(val) {
    this.pageSize = val;
    this.getList(this.currentPage, val);
    // eslint-disable-next-line no-console
    // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList(val, this.pageSize);
      // eslint-disable-next-line no-console
      // console.log(`当前页: ${val}`);
    },

  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-top: 50px;
}
.img-feng {
  width: 20px;
  height: 20px;
}
.ad-style {
  width: 10px;
}
.el-upload-list--picture-card .el-upload-list__item-actions {
  width: 50%;
}
</style>